#ifndef __APP_PROTECT_H__
#define __APP_PROTECT_H__

#include "common_config.h"

#include <stdint.h>

/* 移动平均滤波窗口大小 */
#define ADC_FILTER_WINDOW_SIZE 10U
#define ADC_FILTER_SWITCH      1

/**
 * @brief 保护模块ADC缓冲区索引
 *
 */
typedef enum
{
    ADC_IN_IDX_BAT      = 0U,
    ADC_IN_IDX_FAN      = 1U,
    ADC_IN_IDX_HEATL    = 2U,
    ADC_IN_IDX_NTC1     = 3U,
    ADC_IN_IDX_SW2      = 4U,
    ADC_IN_IDX_ADC0_MAX = 5U,
    ADC_IN_IDX_ADC1_MAX = 3U,
    ADC_IN_IDX_P_V      = 5U,
    ADC_IN_IDX_HEATR    = 6U,
    ADC_IN_IDX_SW1      = 7U,
    ADC_IN_IDX_MAX,
} adc_in_idx_e;

#if ADC_FILTER_SWITCH
/**
 * @brief ADC移动平均滤波器结构体
 *
 */
typedef struct
{
    int32_t buf[ADC_FILTER_WINDOW_SIZE]; /*!< 滤波缓冲区 */
    uint8_t index;                       /*!< 当前写入索引 */
    uint8_t count;                       /*!< 已存储的样本数量 */
    int64_t sum;                         /*!< 当前窗口内数据总和 */
} mov_avg_filter_t;
#endif

/**
 * @brief 保护模块监控结构体
 *
 */
typedef struct
{
    int32_t          bat_ad_real_val;   /*!< 电池电压ADC实时值 */
    int32_t          bat_cal_val;       /*!< 电池电压计算后的值 */
    mov_avg_filter_t bat_filter;        /*!< 电池电压滤波器 */
    int32_t          fanl_ad_real_val;  /*!< 风扇左ADC实时值 */
    int32_t          fanl_cal_val;      /*!< 风扇左计算后的值 */
    mov_avg_filter_t fanl_filter;       /*!< 风扇左滤波器 */
    int32_t          fanr_ad_real_val;  /*!< 风扇右ADC实时值 */
    int32_t          fanr_cal_val;      /*!< 风扇右计算后的值 */
    mov_avg_filter_t fanr_filter;       /*!< 风扇右滤波器 */
    int32_t          heatl_ad_real_val; /*!< 加热左ADC实时值 */
    int32_t          heatl_cal_val;     /*!< 加热左计算后的值 */
    mov_avg_filter_t heatl_filter;      /*!< 加热左滤波器 */
    int32_t          pump_ad_real_val;  /*!< 泵ADC实时值 */
    int32_t          pump_cal_val;      /*!< 泵计算后的值 */
    mov_avg_filter_t pump_filter;       /*!< 泵滤波器 */
    int32_t          valve_ad_real_val; /*!< 阀ADC实时值 */
    int32_t          valve_cal_val;     /*!< 阀计算后的值 */
    mov_avg_filter_t valve_filter;      /*!< 阀滤波器 */
    int32_t          heatr_ad_real_val; /*!< 加热右ADC实时值 */
    int32_t          heatr_cal_val;     /*!< 加热右计算后的值 */
    mov_avg_filter_t heatr_filter;      /*!< 加热右滤波器 */
} monitor_t;

result_e app_protect_init(void);
void     app_protect_handle(void);

#endif
